package com.scm.controller;

/**
 * @author CHL
 * @ClassName: PomainController
 * @Description: TODO
 * @date 2022/3/8 14:32
 * @Version 1.0
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.PoitemMapper;
import com.scm.dao.PomainMapper;
import com.scm.entity.Poitem;
import com.scm.entity.Pomain;
import com.scm.entity.response.ResponseMessage;
import com.scm.service.IPomainService;
import com.scm.util.DateUtil;
import com.scm.util.StringUtil;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.ibatis.transaction.TransactionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/purchase"})
public class PomainController
{
    private ResponseMessage rm;
    @Autowired
    private IPomainService pomainService;
    @Autowired
    private PomainMapper pomainMapper;
    @Autowired
    private PoitemMapper poitemMapper;

    public boolean checkPomain(Pomain pomain)
    {
        Long poId = pomain.getPoId();
        if (poId == null)
        {
            this.rm.setMessage("采购单号poId不能为空");
            return false;
        }
        String account = pomain.getAccount();
        if (StringUtil.isEmpty(account))
        {
            this.rm.setMessage("创建用户account不能为空");
            return false;
        }
        String venderCode = pomain.getVenderCode();
        if (StringUtil.isEmpty(venderCode))
        {
            this.rm.setMessage("供应商编号venderCode不能为空");
            return false;
        }
        String createTime = pomain.getCreateTime();
        if (StringUtil.isEmpty(createTime))
        {
            this.rm.setMessage("createTime不能为空");
            return false;
        }
        if (!DateUtil.isValid(createTime))
        {
            this.rm.setMessage("createTime格式不正确：yyyy-MM-dd HH:mm:ss");
            return false;
        }
        if (StringUtil.isEmpty(pomain.getPayType()))
        {
            this.rm.setMessage("付款方式payType不能为空");
            return false;
        }
        Double productTotal = pomain.getProductTotal();
        if (StringUtil.isEmpty(productTotal))
        {
            this.rm.setMessage("采购产品价productTotal不能为空");
            return false;
        }
        if (productTotal.doubleValue() <= 0.0D)
        {
            this.rm.setMessage("价格productTotal必须为正数");
            return false;
        }
        Double tipFee = pomain.getTipFee();
        if (tipFee.doubleValue() < 0.0D)
        {
            this.rm.setMessage("采购附加费不能为负数");
            return false;
        }
        Double poTotal = pomain.getPoTotal();
        if (poTotal.doubleValue() != productTotal.doubleValue() + pomain.getTipFee().doubleValue())
        {
            this.rm.setMessage("采购总价不正确");
            return false;
        }
        List<Poitem> poitems = pomain.getPoitems();
        if ((poitems == null) || (poitems.size() == 0))
        {
            this.rm.setMessage("产品详细poitems不能为空");
            return false;
        }
        Double total = Double.valueOf(0.0D);
        for (Poitem poitem : poitems)
        {
            Integer num = poitem.getNum();
            if (num.intValue() <= 0)
            {
                this.rm.setMessage("采购产品数据为正数");
                return false;
            }
            Double unitPrice = poitem.getUnitPrice();
            if (unitPrice.doubleValue() <= 0.0D)
            {
                this.rm.setMessage("采购产品为正数");
                return false;
            }
            Double itemPrice = poitem.getItemPrice();
            System.out.println("itemPrice:" + itemPrice);
            double price = new BigDecimal(unitPrice.doubleValue() * num.intValue()).setScale(2, 4).doubleValue();
            if (itemPrice.doubleValue() != price)
            {
                this.rm.setMessage("采购产品价格不正确");
                return false;
            }
            total = Double.valueOf(total.doubleValue() + itemPrice.doubleValue());
        }
        System.out.println("productTotal:" + productTotal + ", total:" + total);
        if (productTotal.doubleValue() != total.doubleValue())
        {
            this.rm.setMessage("采购产品单明细不正确");
            return false;
        }
        if (poTotal.doubleValue() != productTotal.doubleValue() + tipFee.doubleValue())
        {
            this.rm.setMessage("采购产品单总价不正确");
            return false;
        }
        Integer payType = pomain.getPayType();
        if ((payType.intValue() != 1) && (payType.intValue() != 3) && (payType.intValue() != 2))
        {
            this.rm.setMessage("付款不正确");
            return false;
        }
        Double prePayFee = pomain.getPrePayFee();
        if (prePayFee == null)
        {
            this.rm.setMessage("预付款金额不正确");
            return false;
        }
        if (prePayFee.doubleValue() < 0.0D)
        {
            this.rm.setMessage("预付款金额不能为负数");
            return false;
        }
        if (prePayFee.doubleValue() >= poTotal.doubleValue())
        {
            this.rm.setMessage("预付款金额不能大于采购总金额");
            return false;
        }
        if ((payType.intValue() != 3) && (prePayFee.doubleValue() > 0.0D))
        {
            this.rm.setMessage("预付款金额不正确");
            return false;
        }
        Integer status = pomain.getStatus();
        if ((status == null) || (status.intValue() != 1))
        {
            this.rm.setMessage("采购单状态status 应为1表示新增");
            return false;
        }
        return true;
    }

    @RequestMapping({"/pomain/add"})
    public ResponseMessage add(@RequestBody Pomain pomain)
    {
        this.rm = new ResponseMessage();
        boolean flag = checkPomain(pomain);
        if (!flag)
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        this.pomainService.insert(pomain);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("采购单新增成功");
        return this.rm;
    }

    @RequestMapping({"/pomain/update"})
    public ResponseMessage update(@RequestBody Pomain pomain)
    {
        this.rm = new ResponseMessage();
        boolean flag = checkPomain(pomain);
        if (!flag)
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        this.pomainService.update(pomain);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("采购单修改成功");
        return this.rm;
    }

    @RequestMapping({"/pomain/delete"})
    public ResponseMessage delete(Integer page, Long poId)
    {
        this.rm = new ResponseMessage();
        page = Integer.valueOf(page == null ? 1 : page.intValue());
        this.pomainService.delete(poId);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("采购单删除成功");
        this.rm.setData(this.pomainService.selectByStatus(page, Integer.valueOf(1), null));
        return this.rm;
    }

    @RequestMapping({"/pomain/show"})
    public PageInfo<Pomain> select(Integer page, Integer type, Integer payType)
    {
        PageInfo<Pomain> info = this.pomainService.selectByStatus(page, type, payType);
        return info;
    }

    @RequestMapping({"/pomain/query"})
    public PageInfo<Pomain> selectQuery(Integer page, Long poId, String venderCode, Integer payType, String startDate, String endDate, Integer status)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        HashMap<String, Object> m = new HashMap();
        if (!StringUtil.isEmpty(poId)) {
            m.put("poId", poId + "");
        }
        if (!StringUtil.isEmpty(venderCode)) {
            m.put("venderCode", venderCode);
        }
        if (!StringUtil.isEmpty(payType)) {
            m.put("payType", payType);
        }
        if (!StringUtil.isEmpty(startDate)) {
            m.put("startDate", startDate);
        }
        if (!StringUtil.isEmpty(endDate)) {
            m.put("endDate", endDate);
        }
        if (!StringUtil.isEmpty(status)) {
            m.put("status", status);
        }
        List<Pomain> pomains = this.pomainMapper.selectCondition(m);
        PageInfo<Pomain> info = new PageInfo(pomains);

        return info;
    }

    @RequestMapping({"/pomain/queryItem"})
    public List<Poitem> queryItem(Long poId)
    {
        if (poId == null) {
            throw new TransactionException("采购单号不能为空");
        }
        List<Poitem> poitems = this.poitemMapper.selectByPoId(poId.longValue());
        return poitems;
    }

    @RequestMapping({"/pomain/end"})
    public ResponseMessage end(Long poId, Integer page, Integer payType, HttpServletRequest req)
    {
        this.rm = new ResponseMessage();
        String account = (String)req.getAttribute("login");
        HashMap<String, Object> map = new HashMap();
        map.put("account", account);
        map.put("poId", poId);
        map.put("now", DateUtil.currentTime());
        map.put("endStatus", Integer.valueOf(4));
        int n = this.pomainMapper.end(map);
        if (n == 1)
        {
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("了结成功");
            this.rm.setData(this.pomainService.selectByStatus(page, Integer.valueOf(4), payType));
        }
        else
        {
            this.rm.setCode(Integer.valueOf(4));
            this.rm.setMessage("了结成失败");
        }
        return this.rm;
    }
}

